#include "ws2812.h"
#include <math.h>

 RGB_PixelBuffer RGB_Buffer={0}; 

/**
  * [url=home.php?mod=space&uid=247401]@brief[/url]  设置特定LED的RGB颜色。
  * @param  RGB: 组合后的RGB颜色值，格式为0x00RRGGBB。
  * @param  Index: 要写入的LED索引。
  * @retval None.
  */ 
void RGB_Write_Led_Bit(uint32_t RGB, uint16_t Index) {
    uint16_t led_index = Index;
    uint8_t  green= (RGB >> 16);  
    uint8_t  red= (RGB >> 8);  
    uint8_t blue = RGB;        
    for (uint8_t n = 0; n < 8; n++) {
        RGB_Buffer.All_Buffer[led_index][0 * 8 + n] = (red & (0x80 >> n)) ? RGB_led_bit_1 : RGB_led_bit_0;
        RGB_Buffer.All_Buffer[led_index][1 * 8 + n] = (green & (0x80 >> n)) ? RGB_led_bit_1 : RGB_led_bit_0;
        RGB_Buffer.All_Buffer[led_index][2 * 8 + n] = (blue & (0x80 >> n)) ? RGB_led_bit_1 : RGB_led_bit_0;
    }
}


 /**
  * @brief  同时点亮所有LED为相同的RGB颜色。
  * @param  RGB: 组合后的RGB颜色值，格式为0x00RRGGBB。
  * @param  numLeds: LED的数量。
  * @retval None.
  */
void RGB_Write_Led_Bit_AllLedSame(uint32_t RGB, uint16_t numLeds) {
    for (uint16_t i = 0; i < numLeds; i++) {
        RGB_Write_Led_Bit(RGB, i); 
    }
}

// 启动DMA传输
void WS2812_Update(void) {
            if((dma_data_number_get(DMA1_CHANNEL4) != 0))
                {
                        return;
                }else
                {
                         tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_1, 0);                
             tmr_counter_enable(TMR3, FALSE);
             dma_channel_enable(DMA1_CHANNEL4, FALSE);
             dma_data_number_set(DMA1_CHANNEL4, (RGB_PIXEL_NUM * 24)); 
                         dma_interrupt_enable(DMA1_CHANNEL4, DMA_FDT_INT, TRUE);                         
                         tmr_dma_request_enable(TMR3, TMR_C1_DMA_REQUEST, TRUE);        
             dma_channel_enable(DMA1_CHANNEL4, TRUE);
       tmr_counter_enable(TMR3, TRUE); 
                }                        
}

// 将HSV转换为RGB（0~255范围）
uint32_t HSV2RGB(float h, float s, float v) {
    float r, g, b;
    int i;
    float f, p, q, t;
    h = fmodf(h, 360);  // 限制在0-360
    if (s == 0) {
        r = g = b = v;
    } else {
        h /= 60;
        i = floor(h);
        f = h - i;
        p = v * (1 - s);
        q = v * (1 - s * f);
        t = v * (1 - s * (1 - f));
        switch (i) {
            case 0: r = v; g = t; b = p; break;
            case 1: r = q; g = v; b = p; break;
            case 2: r = p; g = v; b = t; break;
            case 3: r = p; g = q; b = v; break;
            case 4: r = t; g = p; b = v; break;
            default:r = v; g = p; b = q; break;
        }
    }
    uint8_t R = (uint8_t)(r * 255);
    uint8_t G = (uint8_t)(g * 255);
    uint8_t B = (uint8_t)(b * 255);
    return (G << 16) | (R << 8) | B;
}

// 用于缓存颜色数据，实现流水移动
#define MAX_LEDS  24  // 根据实际灯带数量修改
uint32_t color_buffer[MAX_LEDS];

// 初始化颜色缓存
void Init_GradientColors(uint16_t numLeds) {
    for (uint16_t i = 0; i < numLeds; i++) {
        float hue = (360.0f * i) / numLeds;
        color_buffer[i] = HSV2RGB(hue, 1.0f, 1.0f);
    }
}

// 滑动颜色数据，制造流水效果
void Shift_GradientColors(uint16_t numLeds) {
    uint32_t temp = color_buffer[numLeds - 1];
    for (int i = numLeds - 1; i > 0; i--) {
        color_buffer[i] = color_buffer[i - 1];
    }
    color_buffer[0] = temp;
}

// 显示当前颜色数组
void Show_GradientColors(uint16_t numLeds) {
    for (uint16_t i = 0; i < numLeds; i++) {
        RGB_Write_Led_Bit(color_buffer[i], i);
    }
    WS2812_Update();
}

// 主函数中调用
void Flowing_Gradient_Effect(uint16_t numLeds) {
    Init_GradientColors(numLeds);
    while (1) {
        Show_GradientColors(numLeds);
        Shift_GradientColors(numLeds);
        wk_delay_ms(30); // 调整速度
    }
}
